<template>
  <div class="registerbox">
    <el-card class="gray-card no-border no-margin-top" shadow="never">
      <el-form ref="form" :model="form" class="form">
        <el-row>
          <el-col :span="12">
            <h2>基准仪器
              <el-select v-model="form.type" placeholder="请选择" :disabled="disabled">
                <el-option
                  v-for="item in typeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </h2>
            <el-col>
              <el-form-item label="仪器名称:" label-width="20%">
                <el-select v-model="form.baseNo" @change="handleBaseChange" placeholder="请选择" :disabled="disabled">
                  <el-option
                    v-for="item in instrumentList"
                    :key="item.instrumentNo"
                    :label="item.instrumentName"
                    :value="item.instrumentNo"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="仪器编码:" label-width="20%">
                <el-input v-model="form.baseNo" class="input-width" disabled/>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="质控品批号:" label-width="20%">
                <el-input v-model="form.baseQualityNo" class="input-width" :disabled="disabled"/>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="试剂批号:" label-width="20%">
                <el-input v-model="form.baseKitNo" class="input-width" :disabled="disabled"/>
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="12">
            <h2>比对仪器</h2>
            <el-col>
              <el-form-item label="仪器名称:" label-width="20%">
                <el-select v-model="form.contrastNo" @change="handleContrastChange" placeholder="请选择"
                           :disabled="disabled">
                  <el-option
                    v-for="item in instrumentList"
                    :key="item.instrumentNo"
                    :label="item.instrumentName"
                    :value="item.instrumentNo"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="仪器编码:" label-width="20%">
                <el-input v-model="form.contrastNo" class="input-width" disabled/>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="质控品批号:" label-width="20%">
                <el-input v-model="form.contrastQualityNo" class="input-width" :disabled="disabled"/>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="试剂批号:" label-width="20%">
                <el-input v-model="form.contrastContrastNo" class="input-width" :disabled="disabled"/>
              </el-form-item>
            </el-col>
          </el-col>
        </el-row>
      </el-form>
      <el-divider/>
      <h2>评价方案</h2>
      <el-form ref="form" :model="form" class="form">
        <el-row>
          <el-col :span="8">
            <span>检验项目
              <el-select v-model="form.projectId"
                         @change="handleChangeProject" placeholder="请选择"
                         :disabled="disabled">
              <el-option
                v-for="item in projectOptions"
                :key="item.id"
                :label="item.projectName"
                :value="item.id"
              >
              </el-option>
            </el-select></span>
            <el-form-item label="线性范围:">
              <el-input v-model="form.baseEaValue" class="td-input" :disabled="disabled"/>
            </el-form-item>

            <el-form-item label="设定质量目标:">
              <el-input v-model="form.baseSetValue" class="td-input" :disabled="disabled"/>
            </el-form-item>

            <el-form-item label="设定质量目标:">
              <el-input v-model="form.differenceValue" class="td-input" :disabled="disabled"/>
            </el-form-item>

          </el-col>
          <el-col :span="8">
            <span>测定样本数量 <el-input v-model="form.sampleNum" placeholder="请输入" :disabled="disabled"/></span>
            <el-form ref="form" :model="form" class="form">
              <el-row>
                <el-form-item label="线性范围:" label-width="20%">
                  <el-input placeholder="上限值" v-model="form.sampleMinValue" :disabled="disabled"/>
                  <el-input placeholder="下限值" v-model="form.sampleMaxValue" :disabled="disabled"/>
                </el-form-item>
              </el-row>
            </el-form>
          </el-col>
          <el-col :span="8">
            <span>达标样本占比单位  <el-input v-model="form.unit" placeholder="单位%" :disabled="disabled"/></span>
            <el-form ref="form" :model="form" class="form">
              <el-row>
                <el-col>
                  <el-form-item label="医学决定水平:" label-width="20%">
                    <el-input placeholder="水平值1" v-model="form.medicineMinValue" :disabled="disabled"/>
                    <el-input placeholder="水平值2" v-model="form.medicineMaxValue" :disabled="disabled"/>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-col>
        </el-row>
      </el-form>
      <el-divider/>
      <h2>结果值</h2>
      <el-button class="fr" type="primary" @click="handleAdd" v-if="!disabled">新增一行</el-button>
      <el-table :data="form.itemsList" style="width: 100%" border>
        <el-table-column label="样本编号" align="center" prop="sampelNo">
          <template slot-scope="scope">
            <el-input v-model="scope.row.sampelNo" :disabled="disabled"/>
          </template>
        </el-table-column>
        <el-table-column label="日立7600" align="center" prop="brandOne">
          <template slot-scope="scope">
            <el-input v-model="scope.row.brandOne" :disabled="disabled"/>
          </template>
        </el-table-column>
        <el-table-column label="诺唯赞QD-S2000" align="center" prop="brandTwo">
          <template slot-scope="scope">
            <el-input v-model="scope.row.brandTwo" :disabled="disabled"/>
          </template>
        </el-table-column>
        <el-table-column label="单位" align="center" prop="unit">
          <template slot-scope="scope">
            <el-input v-model="scope.row.unit" :disabled="disabled"/>
          </template>
        </el-table-column>
        <el-table-column label="相对偏倚" align="center" prop="biasValue">
          <template slot-scope="scope">
            <el-input v-model="scope.row.biasValue" :disabled="disabled"/>
          </template>
        </el-table-column>
        <el-table-column label="R²" align="center" prop="biasR">
          <template slot-scope="scope">
            <el-input v-model="scope.row.biasR" :disabled="disabled"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" v-if="!disabled">
          <template slot-scope="scope">
            <el-button
              @click="addJurisdiction(scope.$index, scope.row)"
              type="primary"
            >复制行
            </el-button>
            <el-button
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              class="del-btn"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-divider/>
      <h2>结论</h2>
      <span>
            <el-input v-model="form.remark" type="textarea" class="textarea" :disabled="disabled"/>
      </span>
      <!--      <h2><el-button type="primary" @click="onSubmit()">生成报告</el-button></h2>-->
      <div class="details-foot">
        <div class="cell-btn">
          <el-button type="primary" @click.native="onSubmit" v-if="!disabled">提交</el-button>
          <el-button @click.native="handleClose">取消</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>

import {listProjectCheckout} from "@/api/business/project"
import {listInstrument} from "@/api/business/instrument"
import {addInstrumentComparison, updateInstrumentComparison} from "@/api/business/instrumentComparison"

export default {
  name: "addEdit",
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      projectOptions: [],
      instrumentList: [],
      typeOptions: [
        {
          label: '设备间对比',
          value: '1'
        },
        {
          label: '检验科对比',
          value: '2'
        }
      ],
      form: {
        type: '',
        sampleMinValue: '',
        sampleMaxValue: '',
        baseName: '',
        baseNo: '',
        baseQualityNo: '',
        baseKitNo: '',
        contrastName: '',
        contrastQualityNo: '',
        contrastContrastNo: '',
        contrastNo: '',
        baseEaValue: '',
        baseSetValue: '',
        differenceValue: '',
        sampleNum: '',
        medicineMinValue: '',
        medicineMaxValue: '',
        remark: '',
        projectName: '',
        unit: '',
        itemsList: []
      }
    };
  },
  created() {
    this.$nextTick(() => {
      this.handleGetItemList()
      this.handleGetInstrument()
    })
  },
  methods: {
    onSubmit() {
      if (this.form.id) {
        updateInstrumentComparison(this.form).then((res) => {
          this.$message.success('操作成功')
          this.handleClose()
        })
      } else {
        addInstrumentComparison(this.form).then((res) => {
          this.$message.success('操作成功')
          this.handleClose()
        })
      }
    },
    handleData(obj, item) {
      this.form = obj;
      this.form.itemsList = item
    },
    handleAdd() {
      let row = {
        sampelNo: '',
        brandOne: '',
        brandTwo: '',
        unit: '',
        biasValue: '',
        biasR: ''
      }
      this.form.itemsList.push(row)
    },
    // 删除一行
    handleDelete(index, row) {
      this.form.itemsList.splice(index, 1)
    },
    // 增加一行
    addJurisdiction(index, row) {
      // 直接push是浅拷贝，只复制了地址过去，改成深拷贝
      this.form.itemsList.push(JSON.parse(JSON.stringify(row)))
    },
    handleBaseChange() {
      this.form.baseName = this.instrumentList.filter(x => x.instrumentNo == this.form.baseNo)[0].instrumentName
    },
    handleContrastChange() {
      this.form.contrastName = this.instrumentList.filter(x => x.instrumentNo == this.form.baseNo)[0].instrumentName
    },
    handleChangeProject() {
      this.form.projectName = this.projectOptions.filter(x => x.id == this.form.projectId)[0].projectName
    },
    handleClose() {
      this.form = {}
      this.$emit('close', false)
    },
    handleGetInstrument() {
      listInstrument().then((res) => {
        this.instrumentList = res.rows
      })
    },
    // 获取检测项
    handleGetItemList() {
      listProjectCheckout().then(res => {
        this.projectOptions = res.rows
      })
    }
  }
}
</script>

<style scoped>

</style>
